import requests
import re
import pandas as pd
import time

def save_data(data,name):
    file_name = name+'_'+time.strftime('%Y_%m_%d',time.localtime(time.time()))+'.csv'
    data.to_csv(file_name,index=None,encoding='utf_8_sig')
    print(file_name+'保存成功')

#地图准确数据
dt = requests.get('https://assets.pyecharts.org/assets/maps/china-cities.js')
dt.encoding = 'utf-8'
data_name = re.findall('name:"(.*?)",',dt.text)
data_id = re.findall('id:"(.*?)",',dt.text)
data = [list(i) for i in zip(data_id,data_name)]

#自己的数据
df = pd.read_csv('all_cities_data_2022_07_19.csv')
dataC = df[['id','name','today_confirm','total_confirm','provinces']].values.tolist()

#系统命名匹配
for i in range(len(dataC)):
    for j in range(len(data)):
        if dataC[i][0] == data[j][0]:
            dataC[i][1] = data[j][1]
        else:
            continue

#删掉自带的州和县和区
for i in range(len(dataC)):
    if (dataC[i][1][-1] =='州' or dataC[i][1][-1] =='县' or dataC[i][1][-1] == '区') and len(dataC[i][1])>2:
        dataC[i][1] = dataC[i][1].strip('州')
        dataC[i][1] = dataC[i][1].strip('县')
        dataC[i][1] = dataC[i][1].strip('区')
print(dataC)



# 重新构建csv文件
csv_data = pd.DataFrame({'name':[dataC[i][1] for i in range(len(dataC))],'provinces':[dataC[i][4] for i in range(len(dataC))],
                         'today_confirm':[dataC[i][2] for i in range(len(dataC))],'total_confirm':[dataC[i][3] for i in range(len(dataC))],})

print(csv_data)
save_data(csv_data,'city-province-data')

#除了地图市的数据
